/**
 * Created with JetBrains WebStorm.
 * User: Artem
 * Date: 26.03.13
 * Time: 13:04
 * To change this template use File | Settings | File Templates.
 */

Ext.define("Pro.controller.CreateTeam", {
    extend: "Ext.app.Controller",

    stores: [
        "Companies",
        "Locations",
        "Stages",
        "Teams"
    ],

    refs: [
        {
            ref: "companyComboBox",
            selector: "createnewteam #companyComboBox"
        },
        {
            ref: "locationComboBox",
            selector: "createnewteam #locationComboBox"
        },
        {
            ref: "stageComboBox",
            selector: "createnewteam #stageComboBox"
        },
        {
            ref: "teamNameTextField",
            selector: "createnewteam #teamNameTextField"
        },
        {
            ref: "createNewTeamButton",
            selector: "createnewteam #createNewTeamButton"
        },
        {
            ref: "createNewTeamForm",
            selector: "createnewteam #createNewTeamForm"
        },
        {
            ref: "createNewTeamPanel",
            selector: "createnewteam"
        },
        {
            ref: "eventLogArea",
            selector: "consolepanel #eventLogArea"
        },
        {
            ref: "requestTab",
            selector: "consolepanel #request"
        },
        {
            ref: "respondTab",
            selector: "consolepanel #respond"
        }
    ],

    init: function(){
        this.control({
            "createnewteam #companyComboBox": {
                change: this.onCompanyComboBoxChanged
            },
            "createnewteam #locationComboBox": {
                change: this.onLocationComboBoxChanged
            },
            "createnewteam #stageComboBox": {
                change: this.onStageComboBoxChanged
            },
            "createnewteam #teamNameTextField": {
                change: this.onTeamNameTextFieldChanged
            },
            "createnewteam #createNewTeamButton": {
                click: this.onCreateNewTeamButtonClick
            }
        });
    },

    onCompanyComboBoxChanged: function(){
        var locationComboBox = this.getLocationComboBox();
        locationComboBox.enable();
    },

    onLocationComboBoxChanged: function(){
        var stageComboBox = this.getStageComboBox();
        stageComboBox.enable();
    },

    onStageComboBoxChanged:function(){
        var teamNameTextField = this.getTeamNameTextField();
        teamNameTextField.enable();
    },

    onTeamNameTextFieldChanged: function(){
        var createNewTeamButton = this.getCreateNewTeamButton();
        var teamNameTextField = this.getTeamNameTextField();
        if (teamNameTextField.value == ""){
            createNewTeamButton.disable();
        } else {
            createNewTeamButton.enable();
        }
    },

    onCreateNewTeamButtonClick: function(){
        var createNewTeamForm = this.getCreateNewTeamForm();
        var thisController = this;
        createNewTeamForm.getForm().submit({
            url: "globallogic/create/team",
            success: function(form, action){
                Ext.MessageBox.alert(localization.MESSAGEBOX_SUCCESS,action.result.message);
                thisController.printRespond(action);
                thisController.printRequest(action);
                thisController.printTable(action);
            },
            failure: function(form, action){
                Ext.MessageBox.alert(localization.MESSAGEBOX_FAILURE,action.result.message);
                thisController.printRespond(action);
                thisController.printRequest(action);
            }
        });
        this.getCreateNewTeamPanel().close();
    },

    printRespond: function(action){
        var respondTab = this.getRespondTab();
        var eventLogArea = this.getEventLogArea();
        eventLogArea.setValue(eventLogArea.getValue() + (new Date()).toLocaleString() + " " + action.result.message + "\n");
        respondTab.setValue(action.response.responseText);
    },

    printRequest: function(action){
        var requestTab = this.getRequestTab();
        var req = "";
        req += ("url:\t\t\t" + action.response.request.options.url + "\n");
        req += ("method:\t\t" + action.response.request.options.method + "\n");
        req += "params: \n"
        req += ("\tcompany:\t\t" + action.response.request.options.params.company + "\n");
        req += ("\tlocation:\t\t" + action.response.request.options.params.location + "\n");
        req += ("\tstage:\t\t" + action.response.request.options.params.stage + "\n");
        req += ("\tname:\t\t" + action.response.request.options.params.name + "\n");
        requestTab.setValue(req);
    }
});